﻿using MediatR;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.Repository;
using Service.Core.Repository.Attribute;
using Service.Gateway.Domain.Events;

namespace Service.Gateway.Application.DomainEventHandlers;

public partial class ChangeGatewayDomainEventHandler(
    ILogger<ChangeGatewayDomainEventHandler> _logger,
    IInternalConfigurationCreator _configurationCreator,
    IFileConfigurationRepository _configurationRepository,
    IInternalConfigurationRepository _internalConfigurationRepository)
    : INotificationHandler<ChangeGatewayDomainEvent>
{


    [UnitOfWork]
    public async Task Handle(ChangeGatewayDomainEvent notification, CancellationToken cancellationToken)
    {
        await _configurationRepository.Set(null);
        var fileConfig = await _configurationRepository.Get();
        var internalConfig = await _configurationCreator.Create(fileConfig.Data);
        _internalConfigurationRepository.AddOrReplace(internalConfig.Data);
    }
}